// hyb_yl/doctorHome/pages/choose_task/choose_task.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tablist: [
      { img: "/hyb_yl/doctorHome/img/tab_img5.png", name: "宣教" },
      { img: "/hyb_yl/doctorHome/img/tab_img5.png", name: "问卷" },
      { img: "/hyb_yl/doctorHome/img/tab_img5.png", name: "检查" },
    ],
    tab_index: 0,
    left_index: 0,
    caozuo: "",
    j_id: "",
    type: "",
    names: "",
    sex: "",
    age: "",
    keyword: "",
    create_time: "",
    type_arrs: [{
      type: "answer",
      title: '问卷'
    }, {
      type: "huanjiao",
      title: "患教"
    }, {
      type: 'care',
      title: "关心提醒"
    }, {
      type: "checklist",
      title: "检查"
    }]
  },
  tab_btn: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      tab_index: index,
      left_px: index * 74 + 40 * (index + 1) + 36,
      type_one: e.currentTarget.dataset.type,
      page: 0,
      list: [],
      check: []
    })
    this.getType_two();
    this.getList();
  },
  left_btn: function (e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    this.setData({
      left_index: index,
      type_two: id,
      page: 0,
      list: [],
      check: []
    })
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var j_id = options.j_id;
    var type = options.type;
    var names = options.names;
    var sex = options.sex;
    var age = options.age;
    var that = this;
    that.setData({
      j_id: j_id,
      type: type,
      names: names,
      sex: sex,
      age: age,
      number: options.number,
      u_thumb: options.u_thumb,
      caozuo: options.caozuo
    })
    that.getType();
    that.getList();
  },
  getType: function (res) {
    var that = this;
    var type = that.data.type;
    app.util.request({
      'url': "entry/wxapp/follow.type",
      data: {
        type: type
      },
      success: function (res) {
        that.setData({
          type_arrs: res.data,
          type_one: res.data[0].id
        })
        that.getType_two();
      }
    })
  },
  getType_two: function (res) {
    var that = this;
    var type_one = that.data.type_one;
    var type = that.data.type;
    app.util.request({
      'url': "entry/wxapp/follow.two_type",
      data: {
        type: type,
        one_type: type_one
      },
      success: function (res) {
        that.setData({
          type_twoarr: res.data,
          type_two: res.data[0].id
        })
        that.getList();
      }
    })
  },
  getList: function () {
    var that = this;
    var type = that.data.type;
    var type_one = that.data.type_one;
    var type_two = that.data.type_two;
    var keyword = that.data.keyword;
    app.util.request({
      'url': "entry/wxapp/follow.get_list",
      data: {
        type_one: type_one,
        type_two: type_two,
        type: type,
        keyword: keyword,
        page: that.data.page,
      },
      success: function (res) {
        that.setData({
          list: res.data,
          page: that.data.page + 1
        })
      }
    })
  },
  changekeyword: function (e) {
    this.setData({
      keyword: e.detail.value,
    })
  },
  search: function () {
    this.setData({
      list: [],
      page: 0,
    })
    this.getList();
  },
  // 取消选择
  quxiao: function () {
    var that = this;
    that.setData({
      check: []
    })
  },
  changestatus: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    list[index].checked = !list[index].checked
    that.setData({
      list: list
    })
  },
  // 保存
  save: function () {
    var that = this;
    var type = that.data.type;
    var caozuo = that.data.caozuo;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    var list = that.data.list;
    var datas = [];
    var data = [];

    for (var i = 0; i < list.length; i++) {
      if (list[i].checked) {
        datas.push(list[i].id);
        data.push(list[i])
      }
    }

    if (type == 'huanjiao') {
      if (caozuo == 'edit') {
        prevPage.setData({
          article_idss: datas,
          articles: data,
        })
      } else if (caozuo == 'add') {
        var article_idss = prevPage.data.article_ids
        var articles = prevPage.data.articles
        if (article_idss.length > 0 && article_idss != '') {
          for (var i = 0; i < article_idss.length; i++) {
            for (var j = 0; j < datas.length; j++) {
              if (article_idss[i] == datas[j]) {
                datas.splice(j, 1);
              }
            }
          }
          datas = datas.concat(article_idss)
          for (var i = 0; i < articles.length; i++) {
            for (var j = 0; j < data.length; j++) {
              if (articles[i].h_id == data[j].h_id) {
                data.splice(j, 1);
              }
            }
          }
          data = data.concat(articles)
        }
        prevPage.setData({
          article_idss: datas,
          articles: data,
        })
      }
    } else if (type == 'answer') {
      if (caozuo == 'edit') {
        prevPage.setData({
          answer_ids: datas,
          answers: data,
        })
      } else if (caozuo == 'add') {
        var answer_ids = prevPage.data.answer_ids
        var answers = prevPage.data.answers
        if (answer_ids.length > 0 && answer_ids != '') {
          for (var i = 0; i < answer_ids.length; i++) {
            for (var j = 0; j < datas.length; j++) {
              if (answer_ids[i] == datas[j]) {
                datas.splice(j, 1);
              }
            }
          }
          datas = datas.concat(answer_ids);

          for (var i = 0; i < answers.length; i++) {
            for (var j = 0; j < data.length; j++) {
              if (answers[i].id == data[j].id) {
                data.splice(j, 1);
              }
            }
          }
          data = data.concat(answers)
        }

        prevPage.setData({
          answer_ids: datas,
          answers: data,
        })
      }

    } else if (type == 'care') {
      if (caozuo == 'edit') {
        prevPage.setData({
          care: datas,
          caress: data,
        })
      } else if (caozuo == 'add') {
        var care = prevPage.data.care
        var caress = prevPage.data.caress
        if (care != '' && care.length > 0) {
          for (var i = 0; i < care.length; i++) {
            for (var j = 0; j < datas.length; j++) {
              if (care[i] == datas[j]) {
                datas.splice(j, 1);
              }
            }
          }
          datas = datas.concat(care);

          for (var i = 0; i < caress.length; i++) {
            for (var j = 0; j < data.length; j++) {
              if (caress[i].id == data[j].id) {
                data.splice(j, 1);
              }
            }
          }
          data = data.concat(caress)
        }
        prevPage.setData({
          care: datas,
          caress: data,
        })
      }

    } else if (type == 'checklist') {

      if (caozuo == 'edit') {
        prevPage.setData({
          checklist: datas,
          checks: data,
        })
      } else if (caozuo == 'add') {
        var checklist = prevPage.data.checklist
        var checks = prevPage.data.checks
        if(checklist.length > 0 && checklist != '')
        {
          for (var i = 0; i < checklist.length; i++) {
            for (var j = 0; j < datas.length; j++) {
              if (checklist[i] == datas[j]) {
                datas.splice(j, 1);
              }
            }
          }
          datas = datas.concat(checklist);
          for (var i = 0; i < checks.length; i++) {
            for (var j = 0; j < data.length; j++) {
              if (checks[i].id == data[j].id) {
                data.splice(j, 1);
              }
            }
          }
          data = data.concat(checks)
        }
        
        
        prevPage.setData({
          checklist: datas,
          checks: data,
        })
      }

    }
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})